/**
 * Created by fupeng on 17/2/17.
 */

var movielibDao = require('../dao/MovielibDao');
var updataFilterByMovie = require('../dao/UpdataFilterByMovie');
var async = require('async');

var cdn = 'http://cdn.bestmovie.com.cn/MOVIE/';
var poster = '/POSTER/';
module.exports = {

    /**
     * 影吧 --> 影吧详情 --> 影吧片库
     * @param req
     * @param res
     * http -- get :framework7 a标签跳转
     * rout:movielib
     */
    getMovielibInfoModel: function (req, res) {

        cinema_code = req.query.cinema_code; //影吧id

        var resultData = {
            message: 'success',
            status: 'success',
            cinemaCode:cinema_code
        };

        res.render('movielib', {
            title: '影吧片库',
            layout: false,
            data: resultData
        });



    },
    /**
     * 影吧片库 影片具体信息加载
     * @param req
     * @param res
     * http -- post -- ajax : framework --> my-app.js 滚动事件触发
     * rout:movielib
     */
    postMovielibInfoModel: function (req, res) {

        var resultData = {};
        var param = {};
        var _param_pageSize = req.body.param_pageSize; //当前页
        if(_param_pageSize === ''){
            _param_pageSize = '0';
        }
        var resultData = {};

        var movieName = '';
        if(!(req.body.movieName == '' || req.body.movieName == undefined)){
            var _movieName = ' and movie.movie_name like "%' + req.body.movieName + '%" ';
            param.movieName = _movieName;
        }else{
            param.movieName = movieName;
        }
        if(!(req.body.param_pageSize == '' || req.body.param_pageSize == undefined)){
            param.page = parseInt(req.body.param_pageSize);
        }else{
            param.page = 0;
        }
        var cinemaCode = '"'+req.body.cinemaCode+'"';
        var sort = ' order by ';                        //排序
        if(req.body.sort == 0){
            sort += 'movietp.publishdate desc';
            // }else if( req.body.sort == 1 ){
            //     sort += '';
        }else if(req.body.sort == 2 ){
            sort += 'movietp.score desc ';
        }else{
            sort += 'movietp.publishdate desc';
        }
        if(!req.body.movieType == '' ){
            var movieTypes = eval(req.body.movieType);
        }else{
            var movieTypes ='';
        }
        var category = '';
        var movieType = '';
        if(movieTypes.length == 1 && movieTypes[0] == 0){
            category = '';
        }else{
            for(var i=0;i < movieTypes.length;i++){
                if(i >= 1){
                    movieType += 'and  movietp.category like "%' + movieTypes[i] + '%" '
                }else{
                    movieType += 'WHERE movietp.category like "%' + movieTypes[i] + '%" ';
                }

            }
            category = movieType;
        }
        var country = req.body.movieCountry;
        var _country = '';
        if(country == '0'){
        }else if(country !== undefined){
            _country = 'and movie.country like "%' + country + '%" ';
        }

        var publishdate = parseInt(req.body.movieYear);
        var _publishdate = '';
        if(!publishdate == 0){
            if (publishdate >= 2011) {
                _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") = ' + publishdate + ' ';
            }
            if (publishdate < 2011 && publishdate >= 2000) {
                _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 2000 and 2010 ';
            }
            if (publishdate < 2000 && publishdate >= 1990) {
                _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 1990 and 2000 ';
            }
            if(publishdate <1990 && publishdate >= 1980 ){
                _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 1980 and 1990 ';
            }
            if(publishdate < 1980){
                _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") < 1980 ';
            }
        }

        param.cinemaCode= cinemaCode;
        param.sort = sort;
        param.category = category;
        param.country = _country;
        param.publishdate = _publishdate;
        //根据_param_pageSize查询影吧
        movielibDao.getMovieInfoByParam(param, function (data) {
            var movieInfo = [];
            if (data.isSuccess) {
                data.rows.forEach(function (val) {

                    movieInfo.push({
                        mid: val.mid,                       //影片编号
                        movie_name: val.movie_name,         //影片名字
                        director: val.director,              //导演
                        brief: val.brief,                    //详情介绍
                        cast: val.cast,                    //演员
                        score: val.score,                    //评分
                        country: val.country,                //地区
                        category: val.category,               //类型-标签
                        publishdate: val.publishdate,        //影片-年
                        poster: cdn+val.mid+poster+val.poster
                    });

                });
                if(movieInfo.length < 10){
                    resultData = {
                        title: '影吧片库',
                        status: 'success',
                        message: '已无更多信息',
                        data: movieInfo

                    };
                }else{
                    resultData = {
                        title: '影吧片库',
                        status: 'success',
                        message: '加载完成',
                        data: movieInfo

                    };
                }

                res.json(resultData);

            } else {

                resultData = {
                    message: 'error',
                    status: 'error',
                    data: ''
                };

                res.json(resultData);
            }

        })

    },
    /**
     * 影片片库 更新 筛选条件 影片类型(标签)
     * @param req
     * @param res
     */
    postMovieFilter: function (req, res) {

        resultData = {};

        async.parallel({
                MovieType: function (callback) {
                    updataFilterByMovie.getMovieType(function (data) {

                        var movieType = [];
                        if (data.isSuccess) {
                            data.rows.forEach(function (val) {

                                movieType.push({
                                    movieType: val.movie_type
                                });

                            });

                            callback(null, movieType);


                        } else {

                            callback(null, '影吧信息有误');

                        }

                    });
                },
                MovieCountry: function (callback) {
                    updataFilterByMovie.getMovieCountry(function (data) {

                        var movieCountry = [];
                        if (data.isSuccess) {
                            data.rows.forEach(function (val) {

                                movieCountry.push({
                                    movieCountry: val.movie_country
                                });

                            });

                            callback(null, movieCountry);


                        } else {

                            callback(null, '影吧信息有误');

                        }

                    });
                }
            },
            function (err, results) {
                if (!err) {
                    if (results.MovieCountry.length > 1 && results.MovieCountry.length > 1) {
                        resultData = {
                            message: 'success',
                            status: 'success',
                            movieType: results.MovieType,
                            movieCountry: results.MovieCountry
                        };
                    }else{
                        resultData = {
                            message : '影片筛选条件查询出现问题',
                            status:'fail'
                        }
                    }
                    res.json(resultData);
                } else {
                    console.log(err);//暂留日志
                }
            });


    },
    /**
     * 影吧片库--搜索影片
     * @param req
     * @param res
     * http -- post -- ajax : framework --> my-app.js 搜索按钮
     * rout:movielibbyparam
     */
    // postMovieLibinfoModelByParam: function (req, res) {
    //     var param = {};
    //     var _param_pageSize = req.body.param_pageSize; //当前页
    //     if(_param_pageSize === ''){
    //         _param_pageSize = '0';
    //     }
    //     var resultData = {};
    //
    //     var movieName = '';
    //     if(!(req.body.movieName == '' || req.body.movieName == undefined)){
    //         var _movieName = ' and movie.movie_name like "%' + req.body.movieName + '%" ';
    //         param.movieName = _movieName;
    //     }else{
    //         param.movieName = movieName;
    //     }
    //     if(!(req.body.param_pageSize == '' || req.body.param_pageSize == undefined)){
    //         param.page = req.body.param_pageSize;
    //     }else{
    //         param.page = 0;
    //     }
    //     var cinemaCode = '"'+req.body.cinemaCode+'"';
    //     var sort = ' order by ';                        //排序
    //     if(req.body.sort == 0){
    //         sort += 'movietp.publishdate desc';
    //         // }else if( req.body.sort == 1 ){
    //         //     sort += '';
    //     }else if(req.body.sort == 2 ){
    //         sort += 'movietp.score desc ';
    //     }
    //     var movieTypes = eval(req.body.movieType);
    //     var category = '';
    //     var movieType = '';
    //     if(movieTypes.length == 1 && movieTypes[0] == 0){
    //         category = '';
    //     }else{
    //         for(var i=0;i < movieTypes.length;i++){
    //             // if(i >= 1){
    //             //     movieType += ',"'+ movieTypes[i]+'"';
    //             // }else{
    //             //     movieType += '"'+ movieTypes[i]+'"';
    //             // }
    //             // category = 'WHERE movie_type.movie_type in (' + movieType + ') ';
    //             if(i >= 1){
    //                 movieType += 'and  movietp.category like "%' + movieTypes[i] + '%" '
    //             }else{
    //                 movieType += 'WHERE movietp.category like "%' + movieTypes[i] + '%" ';
    //             }
    //
    //         }
    //         category = movieType;
    //     }
    //     var country = req.body.movieCountry;
    //     var _country = '';
    //     if(country == '0'){
    //     }else{
    //         _country = 'and movie.country like "%' + country + '%" ';
    //     }
    //
    //     var publishdate = parseInt(req.body.movieYear);
    //     var _publishdate = '';
    //     if(!publishdate == 0){
    //         if (publishdate >= 2011) {
    //             _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") = ' + publishdate + ' ';
    //         }
    //         if (publishdate < 2011 && publishdate >= 2000) {
    //             _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 2000 and 2010 ';
    //         }
    //         if (publishdate < 2000 && publishdate >= 1990) {
    //             _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 1990 and 2000 ';
    //         }
    //         if(publishdate <1990 && publishdate >= 1980 ){
    //             _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") BETWEEN 1980 and 1990 ';
    //         }
    //         if(publishdate < 1980){
    //             _publishdate = 'and DATE_FORMAT(movie.publishdate,"%Y") < 1980 ';
    //         }
    //     }
    //
    //     param.cinemaCode= cinemaCode;
    //     param.sort = sort;
    //     param.category = category;
    //     param.country = _country;
    //     param.publishdate = _publishdate;
    //     movielibDao.getMovieInfoByParam(param, function(data){
    //
    //         var movieInfo = [];
    //
    //         if (data.isSuccess) {
    //
    //             data.rows.forEach(function (val) {
    //
    //                 movieInfo.push({
    //                     mid: val.mid,                          //影片编号
    //                     movie_name: val.movie_name,         //影片名字
    //                     director: val.director,              //导演
    //                     brief: val.brief,                    //详情介绍
    //                     cast: val.cast,                      //演员
    //                     score: val.score,                    //评分
    //                     country: val.country,                //地区
    //                     category: val.category,              //类型-标签
    //                     publishdate: val.publishdate,          //影片-年
    //                     poster: cdn+val.mid+poster+val.poster
    //                 });
    //
    //             });
    //
    //             if(movieInfo.length != 0){
    //
    //
    //                 cb(null,movieInfo);
    //             }else{
    //                 cb({
    //                     message: 'fail',
    //                     status: 'success'
    //                     // param_pageSize: (_param_pageSize+1),
    //                 },null);
    //
    //             }
    //
    //         } else {
    //
    //             resultData = {
    //                 message: 'error',
    //                 status: '影院片库->影片查询出错',
    //                 data: ''
    //             };
    //             cb(resultData,'');
    //         }
    //     });
    //
    //
    //
    //     // movielibDao.getMovieInfoByParam(param,  function (data) {
    //     //
    //     //     var movieInfo = [];
    //     //
    //     //     if (data.isSuccess) {
    //     //         data.rows.forEach(function (val) {
    //     //
    //     //             movieInfo.push({
    //     //                 mid: val.mid,                          //影片编号
    //     //                 movie_name: val.movie_name,         //影片名字
    //     //                 director: val.director,              //导演
    //     //                 brief: val.brief,                    //详情介绍
    //     //                 cast: val.cast,                      //演员
    //     //                 score: val.score,                    //评分
    //     //                 country: val.country,                //地区
    //     //                 category: val.category,              //类型-标签
    //     //                 publishdate: val.publishdate,          //影片-年
    //     //                 poster: cdn+val.mid+poster+val.poster
    //     //             });
    //     //
    //     //         });
    //     //
    //     //         if(movieInfo.length != 0){
    //     //             resultData = {
    //     //                 message: 'success',
    //     //                 status: 'success',
    //     //                 param_pageSize: (_param_pageSize + 1),
    //     //                 totalPage:data.rows.length,
    //     //                 data: movieInfo
    //     //             };
    //     //         }else{
    //     //             resultData = {
    //     //                 message: 'fail',
    //     //                 status: 'success'
    //     //                 // param_pageSize: (_param_pageSize+1),
    //     //             };
    //     //         }
    //     //         res.json(resultData);
    //     //
    //     //     } else {
    //     //
    //     //         resultData = {
    //     //             message: 'error',
    //     //             status: 'error',
    //     //             data: ''
    //     //         };
    //     //
    //     //         res.json(resultData);
    //     //     }
    //     //
    //     // });
    // }
};


